package com.engine.topyoung.duss.afterlogin;

import com.alibaba.fastjson.JSONObject;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-03-10
 * @Description: 校验是否是第一次登录
 * @ModifyLog:
 **/

public class WeaAfterLoginWeb {

    /**
     *@Description: 校验是否是首次登录
     *@Param: [request, response]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2021/3/10
     */
    @GET
    @Path("/isFirstLogin")
    @Produces({MediaType.TEXT_PLAIN})
    public String isFirstLogin(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser( request, response );
            int uid = user.getUID();
            RecordSet rs = new RecordSet();
            boolean falg = false;
            String sql = "select a.field36 from cus_fielddata a where a.scope = 'HrmCustomFieldByInfoType' and scopeid=-1 and a.id = ?";
            rs.executeQuery( sql,uid );
            rs.next();
            //是否首次登录
            String field36 = Util.null2String( rs.getString("field36"));
            if("".equals( field36 )){
                String scope = "HrmCustomFieldByInfoType";
                String scopeid="-1";
                String sql1 = "insert into cus_fielddata (scope,scopeid,field36,id) values(?,?,?,?)";
                Object[] params = {scope,scopeid,'1',uid};
                rs.executeUpdate( sql1 ,params);
                falg = true;
            }else if("0".equals( field36 )){
                String sql1 = "update cus_fielddata set field36=1 where id=?";
                rs.executeUpdate( sql1,uid );
                falg = true;
            }
            apidatas.put( "falg", falg );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "error_message", "请求异常" );
        }
        return JSONObject.toJSONString( apidatas );
    }

}
